/**
 * Created by yuris on 10.02.15.
 */
package controller {
import InterfaceObjects.ShopObjectContextMenu;
import InterfaceObjects.WallObjectContextMenu;

import flash.geom.Point;

import model.Constants;

import model.Constants;
import model.PrototypeProxy;
import model.RuntimeProxy;
import model.UserProxy;
import model.objects.ProductWarehouseData;
import model.objects.ShelfData;
import model.objects.ShopObjectData;
import model.prototypes.ProductProto;

import mvcexpress.mvc.PooledCommand;


public class InterfaceLayerTouchCommand extends PooledCommand {
    [Inject]
    public var userProxy:UserProxy;

    [Inject]
    public var prototypeProxy:PrototypeProxy;

    [Inject]
    public var runtimeProxy:RuntimeProxy;

    private var _bufPoint:Point = new Point();
    public function execute(touchData:Object):void {
        switch (touchData.type){
            case Constants.TOUCH_BOTTOM_PANEL_INTERIOR_MODE_BUTTON:
                runtimeProxy.setMode(Constants.MODE_INTERIOR, null);
                break;
            case Constants.TOUCH_BOTTOM_PANEL_SIMULATION_MODE_BUTTON:
                runtimeProxy.contextMenuTarget = null;
                runtimeProxy.setMode(Constants.MODE_SIMULATION, null);
                break;
            case Constants.TOUCH_BOTTOM_PANEL_WAREHOUSE_ITEM:
                this._processWarehouseTouch(touchData.cell_num, touchData.item_pos);
                break;
            case Constants.TOUCH_BOTTOM_PANEL_FLOORPAINT:
                runtimeProxy.setMode(Constants.MODE_INTERIOR_PLACE_FLOOR, {full_sprite_name:Constants.PATH_FLOOR_SPRITES+"/"+prototypeProxy.getFloorProto(touchData.item_id).spriteId, floor_id:touchData.item_id});
                break;
            case Constants.TOUCH_BOTTOM_PANEL_WALLPAINT:
                runtimeProxy.setMode(Constants.MODE_INTERIOR_PLACE_WALL, {full_sprite_name:Constants.PATH_WALL_SPRITES+"/"+prototypeProxy.getWallProto(touchData.item_id).spriteId, wall_id:touchData.item_id});
                break;
            case Constants.TOUCH_BOTTOM_PANEL_SHELFS:
                var shopObject:ShopObjectData = new ShelfData(prototypeProxy.getShelfProto(touchData.item_id));
                shopObject.angle = 225;
                runtimeProxy.setMode(Constants.MODE_INTERIOR_PLACE_SHELF, {object:shopObject, angle:225, shelf_id:touchData.item_id});
                break;
            case Constants.TOUCH_ORDER_PRODUCT_WINDOW_ITEM:
                    this._processProductOrder(touchData.id, touchData.item_pos);
                break;
            case Constants.TOUCH_ORDER_PRODUCT_CLOSE_BUTTON:
                    runtimeProxy.setMode(Constants.MODE_SIMULATION, {});
                    //runtimeProxy.orderProductWindowVisibility = false;
                    //runtimeProxy.bottomPanelLock = false;
                break;
            case Constants.TOUCH_BOTTOM_PANEL_CLOSE_BUTTON:
                    if(runtimeProxy.mode.indexOf(Constants.MODE_INTERIOR) >= 0){
                        runtimeProxy.setMode(Constants.MODE_INTERIOR, null);
                        runtimeProxy.removeAllHighlights();
                    } else {
                        runtimeProxy.setMode(Constants.MODE_SIMULATION, null);
                    }
                break;
            case ShopObjectContextMenu.ROTATE_LEFT_BUTTON_TRIGGERED:
                this._processRotateShopObject(runtimeProxy.contextMenuTarget, runtimeProxy.contextMenuTarget.angle - 90);
                break;
            case ShopObjectContextMenu.ROTATE_RIGHT_BUTTON_TRIGGERED:
                this._processRotateShopObject(runtimeProxy.contextMenuTarget, runtimeProxy.contextMenuTarget.angle + 90);
                break;
            case ShopObjectContextMenu.SELL_BUTTON_TRIGGERED:
                runtimeProxy.removeShopObject(runtimeProxy.contextMenuTarget);
                runtimeProxy.contextMenuTarget = null;
                break;
            case ShopObjectContextMenu.MOVE_BUTTON_TRIGGERED:
                runtimeProxy.removeShopObject(runtimeProxy.contextMenuTarget);
                runtimeProxy.setMode(Constants.MODE_INTERIOR_MOVE_SHELF,{object:runtimeProxy.contextMenuTarget})
                runtimeProxy.highlightOccMatrix(runtimeProxy.contextMenuTarget.rotatedOCCMatrix, runtimeProxy.contextMenuTarget.position);
                runtimeProxy.contextMenuTarget = null;
                break;
            case WallObjectContextMenu.MOVE_BUTTON_TRIGGERED:
                if(runtimeProxy.wallObjectContextMenuTarget != null){
                    this._bufPoint.copyFrom(runtimeProxy.wallObjectContextMenuTarget.quad as Point);
                    var _modeParamsBuf:Object = {
                        object:runtimeProxy.wallObjectContextMenuTarget,
                        wall_covers:{x:runtimeProxy.shopDesign.wallCoverDataX, y:runtimeProxy.shopDesign.wallCoverDataY},
                        doors:{x:runtimeProxy.shopDesign.doorDataX,y:runtimeProxy.shopDesign.doorDataY}
                    }

                    if(runtimeProxy.wallObjectContextMenuTarget.is_door){
                        runtimeProxy.shopDesign.setDoorAtQuad(this._bufPoint,null);
                        runtimeProxy.setMode(Constants.MODE_INTERIOR_MOVE_DOOR, _modeParamsBuf);
                    } else {
                        runtimeProxy.shopDesign.setWallCoverAtQuad(this._bufPoint,null);
                        runtimeProxy.setMode(Constants.MODE_INTERIOR_MOVE_WALL_COVER, _modeParamsBuf);
                    }
                    runtimeProxy.wallObjectContextMenuTarget = null;
                }
                break;
            case WallObjectContextMenu.SELL_BUTTON_TRIGGERED:
                if(runtimeProxy.wallObjectContextMenuTarget != null){
                    this._bufPoint.copyFrom(runtimeProxy.wallObjectContextMenuTarget.quad as Point);
                    if(runtimeProxy.wallObjectContextMenuTarget.is_door){
                        if(runtimeProxy.shopDesign.doorsCount > 1){
                            runtimeProxy.shopDesign.setDoorAtQuad(this._bufPoint,null);
                        } else {
                            //TODO: say that last door can't be sold
                        }
                    }else{
                        runtimeProxy.shopDesign.setWallCoverAtQuad(this._bufPoint,null);
                    }
                    runtimeProxy.wallObjectContextMenuTarget = null;
                }
                break;
        }
    }

    private function _processRotateShopObject(shopObject:ShopObjectData, newAngle:int):void {
        if(runtimeProxy.rotateGameObject(shopObject,newAngle)){

        } else {
            //TODO: animate no-placed case
        }
    }

    private var _bufProductWarehouseData:ProductWarehouseData;
    private var _currentTimeBuf:int;
    //private var _orderProductCellNum:int = 0;
    //private var _orderProductCellPosition:Point;
    private function _processWarehouseTouch(cellNum:int, itemPosition:Point):void {
        this._currentTimeBuf = runtimeProxy.currentTime;

        this._bufProductWarehouseData = userProxy.warehouseDataProxy.getProductData(cellNum);
        //--------
        if(this._bufProductWarehouseData == null){//---------- order new product
            //runtimeProxy.bottomPanelLock = true;
            runtimeProxy.setMode(Constants.MODE_SIMULATION_ORDER_PRODUCT_WINDOW, {cell_position: itemPosition, cell_num:cellNum});
            //runtimeProxy.orderProductWindowVisibility = true;
            //this._orderProductCellNum = cellNum;
            //this._orderProductCellPosition = itemPosition;

        } else {//-------------------------- do place product
            runtimeProxy.setMode(Constants.MODE_SIMULATION_PLACE_PRODUCT, {product:this._bufProductWarehouseData, position: itemPosition, cell_num:cellNum});
        }
    }

    private var _bufProductProto:ProductProto;
    private function _processProductOrder(id:String, itemPosition:Point):void {


        runtimeProxy.setOrderProductAnimationCoords(prototypeProxy.getProductProto(id).spriteId,itemPosition,runtimeProxy.modeParams.cell_position);
        //runtimeProxy.orderProductWindowVisibility = false;



        this._currentTimeBuf = runtimeProxy.currentTime;
        this._bufProductProto = prototypeProxy.getProductProto(id);

        this._bufProductWarehouseData = new ProductWarehouseData(this._bufProductProto,this._currentTimeBuf + int(this._bufProductProto.deliver));
        this._bufProductWarehouseData.count = int(userProxy.warehouseDataProxy.volume/this._bufProductWarehouseData.proto.volume);
        this._bufProductWarehouseData.deliverTimeRest = this._bufProductWarehouseData.deliverTime  - this._currentTimeBuf;
        userProxy.warehouseDataProxy.updateCellData(runtimeProxy.modeParams.cell_num,this._bufProductWarehouseData);
        //runtimeProxy.bottomPanelLock = false;

        runtimeProxy.setMode(Constants.MODE_SIMULATION, {});
        //TODO: work on it
    }
}
}
